package com.hyk.commonLib.common.utils.rx.activityResult;

import android.content.Intent;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.hyk.commonLib.common.fragment.BaseFragment;

import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleOnSubscribe;

class RxActivityResultInFragment extends RxActivityResult<BaseFragment<?>> {
    protected RxActivityResultInFragment(BaseFragment<?> fragment) {
        super(fragment);
    }

    @Override
    public Single<ActivityResultCompat> start(Intent intent) {
        ActivityResultContracts.StartActivityForResult activityForResult = new ActivityResultContracts.StartActivityForResult();

        return Single.create((SingleOnSubscribe<ActivityResult>) emitter -> {
            ActivityResultLauncher<Intent> launcher = container.registerForActivityResult(activityForResult, emitter::onSuccess);
            launcher.launch(intent);
        }).map(ActivityResultCompat::convertFromActivityResult);
    }
}
